package com.chzc.fm.duotin.adapter.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.mine.SpecialBean;
import com.chzc.fm.duotin.ui.mine.DownloadMangerActivity;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.chzc.fm.duotin.utils.LogUtil;
import com.chzc.fm.duotin.utils.NumberUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;
import java.util.Vector;

/**
 * Created by BillSinla on 2016/7/7.
 */
public class MangerRecyclerAdapter extends RecyclerView.Adapter<MangerRecyclerAdapter.MangerHolder>{

    private Context context;
    private List<SpecialBean> specialBeanList;
    //private
    private LayoutInflater inflater;

    private Vector<Boolean> vector;

    private OnStateChangeListener onStateChangeListener;

    public MangerRecyclerAdapter(Context context, List<SpecialBean> specialBeanList) {
        this.context = context;
        this.specialBeanList = specialBeanList;
        inflater = LayoutInflater.from(context);
        vector = new Vector<>();
        for(int i = 0; i<specialBeanList.size(); i++){
            vector.add(false);
        }
    }


    public Vector<Boolean> getVector() {
        return vector;
    }

    public void setVector(Vector<Boolean> vector) {
        this.vector = vector;
        notifyDataSetChanged();
    }

    public void setOnStateChangeListener(OnStateChangeListener onStateChangeListener) {
        this.onStateChangeListener = onStateChangeListener;
    }

    @Override
    public MangerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.adapter_mine_item, null);
        return new MangerHolder(view);
    }

    @Override
    public void onBindViewHolder(MangerHolder holder, int position) {
        holder.bindHolder(position);
    }

    @Override
    public int getItemCount() {
        return specialBeanList != null ? specialBeanList.size() : 0;
    }

    public interface OnStateChangeListener{
        void onStateChange(Boolean state);
    }

    protected class MangerHolder extends RecyclerView.ViewHolder{
        public TextView textTitle, textSpace, textNum, textCheck;
        public ImageView imageAvatar;
        public View viewLine;
        public MangerHolder(View itemView) {
            super(itemView);
            textTitle = (TextView) itemView.findViewById(R.id.tv_mine_title);
            textSpace = (TextView) itemView.findViewById(R.id.tv_mine_space);
            textNum = (TextView) itemView.findViewById(R.id.tv_mine_load_num);
            textCheck = (TextView) itemView.findViewById(R.id.tv_mine_check);
            imageAvatar = (ImageView) itemView.findViewById(R.id.iv_mine_avatar);
            viewLine = itemView.findViewById(R.id.v_mine_line);
        }
        public void bindHolder(final int position){
            SpecialBean.AlbumBean albumBean = specialBeanList.get(position).getAlbum();
            List<SpecialBean.ContentListBean> contentList = specialBeanList.get(position).getContent_list();
            textTitle.setText(albumBean.getTitle());
            long space = 0;
            for (SpecialBean.ContentListBean content:contentList) {
                space += Long.parseLong(content.getAudio_32_size());
            }
            LogUtil.w("MineRecyclerAdapter: space:" + space);
            textSpace.setText("占用空间：" + NumberUtil.countSpace(space));
            textNum.setText("已下载：" + contentList.size());

//            File file = new File(FileUtils.DIR_IMAGE, FileUtils.getFileNameByHashCode(albumBean.getImage_url()));
//            if(file.exists()){
//                Bitmap bitmap = BitmapFactory.decodeFile(file.toString());
//                imageAvatar.setImageBitmap(bitmap);
//            }

            ImageLoader.getInstance().displayImage(albumBean.getImage_url(), imageAvatar, ImageUtil.getRoundOptions());
            if(position == specialBeanList.size() - 1){
                viewLine.setVisibility(View.GONE);
            }
            textCheck.setVisibility(View.VISIBLE);

            if(vector.get(position)){
                textCheck.setSelected(true);
            }else{
                textCheck.setSelected(false);
            }

            textCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Click(position);
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Click(position);
                }
            });
        }

        private void Click(int position){
            if(textCheck.isSelected()){
                textCheck.setSelected(false);
                vector.set(position, false);
            }else{
                textCheck.setSelected(true);
                vector.set(position, true);
            }
            boolean flag = false;
            for(int i = 0; i<vector.size(); i++){
                if(i == 0){
                    flag = vector.get(i);
                }else{
                    if(flag != vector.get(i)){
                        flag = false;
                    }
                }
            }
            if(onStateChangeListener != null) {
                onStateChangeListener.onStateChange(flag);
            }
        }
    }

}
